<?php
/**
 * 模型名称(models)
 * template_model.php
 * ============================================================
 * ============================================================
 * 作者：李超 （lichao1005@126.com）v1.0.0
 * 时间：2012-10
 */
class Template_model extends CI_Model
{
	/**
	 *	构造函数
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->tb = 'shop_info';
	}
	
	/**
	 *	添加数据
	 */
	function insert($data)
	{
		$this->db->insert($this->tb, $data);
		return $this->db->insert_id();
	}
	
	/**
	 *	修改数据
	 */
	function update($data, $where)
	{
		$this->db->where($where);
		$this->db->update($this->tb, $data);
		return $this->db->affected_rows();
	}
	
	/**
	 *	删除数据
	 */
	function delete($data)
	{
		$this->db->where($data);
		$this->db->delete($this->tb); 
		return $this->db->affected_rows();
	}
	
	/**
	 *	查询并返回一条数据
	 *  $type:返回结果类型，默认为obj格式，arr为数组格式
	 */
	function row($data,$type='obj')
	{
		$this->db->where($data);
		$query = $this->db->get($this->tb);
		if($type=='obj')return $query->row();
		elseif($type=='arr')return $query->row_array();
	}
	
	 /**
	 *	查询并返回多条数据
	 *  $type:返回结果类型，默认为obj格式，arr为数组格式
	 *  $where为数组，array(field => value)
	 *  $where：查询条件
	 *  $num：单页显示的条数
	 *  $page：当前页数
	 *  $orderby：排序条件
	 */
	function result($where, $type='obj', $num = 10, $page = 1, $orderby = "")
	{
		if($page==0) $page=1;
		$offset=($page-1)*$num;
		$this->db->where($where);
		$this->db->order_by($orderby);
		$query = $this->db->get($this->tb,$num,$offset);
		if($type=='obj')return $query->result();
		elseif($type=='arr')return $query->result_array();
	}

	
	/**
	 *	查询数据条数
	 */
	function num_rows($data)
	{
		$this->db->where($data); 
		$query = $this->db->get($this->tb);
		return $query->num_rows();
	}
}
